﻿using UnityEngine;

public static class UITools
{
    /// <summary>
    /// 打开指定类型的界面
    /// </summary>
    /// <typeparam name="T">界面类型，必须继承自UIViewBase</typeparam>
    /// <param name="layer">UI层级，默认Main</param>
    public static T OpenView<T>(UILayer layer = UILayer.Main) where T : UIViewBase
    {
        var uiManager = GetUIManager();
        if (uiManager == null) return null;

        var view = UIViewBase.InstantiateView<T>();
        if (view != null)
        {
            uiManager.ShowView(view, layer);
        }
        return view;
    }

    /// <summary>
    /// 关闭指定类型的界面
    /// </summary>
    /// <typeparam name="T">界面类型，必须继承自UIViewBase</typeparam>
    public static void CloseView<T>() where T : UIViewBase
    {
        var uiManager = GetUIManager();
        if (uiManager == null) return;

        uiManager.CloseView<T>();
    }

    /// <summary>
    /// 获取已存在的界面实例
    /// </summary>
    /// <typeparam name="T">界面类型，必须继承自UIViewBase</typeparam>
    public static T GetView<T>() where T : UIViewBase
    {
        var uiManager = GetUIManager();
        if (uiManager == null) return null;

        return uiManager.GetView<T>();
    }

    /// <summary>
    /// 获取UIManager实例
    /// </summary>
    private static UIManager GetUIManager()
    {
        return GlobalRefs.AppManager?.GetManager<UIManager>();
    }
}